#! /bin/bash

cur_dir=$(pwd)
source .env
source ../common.sh

LINE_STR="==============================>>>"
echo "${LINE_STR} commands available to users: "
echo "bash startup.sh: 启动容器"
echo "bash startup.sh start: 启动容器"
echo "bash startup.sh save: 保存镜像"
echo "bash startup.sh load: 加载镜像" 
echo "bash startup.sh config: 查看docker配置" 

function start() {
  docker-compose down
  docker-compose up -d
}

# 初始化路径
if [ ! -d "${MINIO_DATA_DIR}/loki-data" ]; then
  mkdir -p ${MINIO_DATA_DIR}/loki-data
  mkdir -p ${MINIO_DATA_DIR}/loki-ruler
fi

# 保存镜像
function imageSave() {
  echo "${LINE_STR} before save image"
  if [ -f "$PWD/minio.tar.gz" ]; then
    rm -rf minio.tar.gz
  fi
  docker save ${image_tag} | gzip > minio.tar.gz
  echo "${LINE_STR} after save image"
}

# 加载镜像
function imageLoad() {
  echo "${LINE_STR} before load image"
  gunzip -c minio.tar.gz | docker load
  echo "${LINE_STR} after load image"
}

echo "input command: $1"
if [ "$1" == "start" ]; then
  start 
elif [ "$1" == "save" ]; then
  imageSave
elif [ "$1" == "load" ]; then
  imageLoad
elif [ "$1" == "config" ]; then
  docker-compose config
else
  start
fi
